#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<cstring>
#include<cstdlib>

using namespace std;

void a()
{
	float wgt = 0;
	char ch;
	while (cin >> wgt >> ch)
	{
		cout << 20 + ceil((wgt - 1)) + (ch == 'y' ? 5 : 0);
	}
}

void b()
{
	int n;
	while (cin >> n)
	{
		vector<int> cost(n + 10);
		for (int i = 0; i < n; i++)
			cin >> cost[i];

		vector<int> dp(n + 10);
		//dp[0] = cost[0]; dp[1] = cost[1];
		for (int i = 2; i <= n; i++)
		{
			dp[i] = min(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1]);
		}
		cout << dp[n] << endl;
	}
}

void c()
{
	int max = 0x3f3f3f3f;
	int n;
	string s1, s2;
	while (cin >> n)
	{
		vector<string> str(n + 10);
		cin >> s1 >> s2;
		for (int i = 0; i < n; i++)
			cin >> str[i];

		int l = 0, r = 0, ans = max;
		//int l = 0, r = 0, ans = max;
		while (s1 != str[l] && l < n)
			l++;
		while (s2 != str[r] && r < n)
			r++;
		if (l < n && r < n)
			ans = min(ans, abs(r - l));
		else
		{
			cout << -1 << endl;
			return;
		}

		while (l < n && r < n)
		{
			for (l++; l < n && s1 != str[l]; l++);
			if (l < n && r < n)
				ans = min(ans, abs(r - l));
			for (r++; s2 != str[r] && r < n; r++);
			if (l < n && r < n)
				ans = min(ans, abs(r - l));
		}
		cout << ans << endl;
	}
}

int main()
{
	//a();
	//b();
	c();
	return 0;
}
